local shopradius = 100
local sells = {}
local shoptype = {}
local pricessell = {{}}
local pricesbuy = {{}}
shoptype[1] = "Muhammad Ali"
shoptype[2] = "Franz Kafka"
shoptype[3] = "Barkeeper"
shoptype[4] = "Best buy, offer now!"
sells[1] = {"Astroid1_result","Petrol","Fruits"}
sells[2] = {"Petrol"}
sells[3] = {"Beer"}
sells[4] = {"Fruits"}
pricessell[1] = {15,0,0}
pricesbuy[1] = {0,60,30}
pricessell[2] = {90}
pricesbuy[2] = {0}
pricessell[3] = {40}
pricesbuy[3] = {0}
pricessell[4] = {90}
pricesbuy[4] = {0}
tradegoods = {}
for index,shop2 in pairs(sells) do
	for _,good in pairs(sells[index]) do
		if 	not (table.HasValue(tradegoods,good)) then
			table.insert(tradegoods,good)
		end
	end
end
local allshops = {}
local sendtext = ""
local sendlist = {}

function RegisterTraders()
	local numberofshop = 1
	allshops = {}
	for k,Shops in pairs(shoptype) do
		shopsoftype = ents.FindByName(Shops)
		for k,Type in pairs(shopsoftype) do
			addnew3dtext("Trader",Type:GetPos() + Vector(0,0,70))
			allshops[numberofshop] = Type
			numberofshop = numberofshop +1
		end
	end
	Msg ("Registerd "..table.getn(allshops).." Traders\n")
	local Spawnpoint = ents.FindByName("Spawnpoint")
	
	Msg ("Registerd "..table.getn(tradegoods).." Tradegoods\n")
end
hook.Add( "InitPostEntity", "RegisterTraders", RegisterTraders );  

function Starttrade(ply)
pos = ply:GetPos()
	for k, shop in pairs (allshops) do
		if (shop:IsValid()) then
			distance = (shop:GetPos()):Distance( pos )
			if (distance < shopradius) then 
				for Index, foundshop in pairs (shoptype) do	
					if (foundshop == shop:GetName( )) then
						local rp = RecipientFilter()
						rp:AddPlayer( ply )
						umsg.Start("TradeSystem", rp) 
						umsg.String( "Headerstart" )
						umsg.End()  
						umsg.Start("TradeSystem", rp) 
						umsg.String("Welcome to the ".. shoptype[Index])
						umsg.End()  
						umsg.Start("TradeSystem", rp) 
						umsg.String("Liststart")
						umsg.End()
						for Number, stuff in pairs (sells[Index]) do
							umsg.Start("TradeSystem", rp) 
							umsg.String(Translate(stuff))
							umsg.Long(pricessell[Index][Number])
							umsg.Long(pricesbuy[Index][Number])
							umsg.End()	
						end
						umsg.Start("TradeSystem", rp) 
						umsg.String( "Stop" )
						umsg.End()
						break
					end
				end
			else
				ply:PrintMessage(HUD_PRINTCENTER, "No Shop Near" )
			end
		end
	end
end
concommand.Add("TradeInitialize",Starttrade)


function Transfer2(ply,command,args) 
	local item = TranslateBack(args[1])
	local sellbuy =args[2]
	pos = ply:GetPos()
	for k, shop in pairs (allshops) do
		if (shop:IsValid()) then
			distance = (shop:GetPos()):Distance( pos )
			if (distance < shopradius) then 
				for Index, foundshop in pairs (shoptype) do	
					if (foundshop == shop:GetName( )) then
						if (args[2] == "Buy") then
							local spawnpoint
							local number2
							for number, stuff in pairs (sells[Index]) do
								if (args[1] == stuff) then
									number2 = number
									local entities = ents.FindInSphere( shop:GetPos(), 400 )
									for _, ent in pairs (entities) do	
										if (ent:GetName() == "Trader_Spawn") then
											spawnpoint = ent
										end	
									end
								end
							end
							if (spawnpoint != nil) then
								if (CurTime( ) < ply.nextspawnprop) then 	
									ply:PrintMessage(HUD_PRINTCENTER, "Stop Spamming, Asshole" ) 
								else
									if (Changemoney(ply,-pricesbuy[Index][number2])) then
										local 	Buyedentity = ents.Create( args[1] )
										Buyedentity:SetAngles(Vector(0,0,0))
										Buyedentity:SetPos(spawnpoint:GetPos())
										Buyedentity:Spawn()
										Buyedentity.valid = true
										if ( Buyedentity:GetPhysicsObject():IsValid() ) then
											Buyedentity:Activate()
											ply.nextspawnprop = CurTime( ) + 0.5
											local entid = Buyedentity:EntIndex() 
											servertable["entitys"][entid] = {}
											servertable["entitys"][entid]["owner"] = ply:UniqueID( )
										end
									end
								end
								return
							else
								ply:PrintMessage(HUD_PRINTCENTER, "No Spawnpoint! Please notify the Admin!" )
								return
							end
						elseif (args[2] == "Sell") then
							for number, stuff in pairs (sells[Index]) do
								if (args[1] == stuff) then
									local entities = ents.FindInSphere( shop:GetPos(), 400 )
									for _, ent in pairs (entities) do	
										if (ent:GetClass() == args[1]) then
											if (ent.valid) then
												if  IsOwner(ent,ply) then
													Changemoney(ply,pricessell[Index][number])
													ent:Remove()
												end
											else
												for _, v in pairs(player.GetAll()) do 
													v:PrintMessage(HUD_PRINTCENTER, (ply:GetName() .. "is a fucking Cheater \n yes everyone know now, you are ruined"))
												end  
												Msg("Player " .. ply:GetName() .. " tryed to sell cheated Entities")
												break
											end
										end	
									end
									return
								end
							end
						end
					end
				end
			else
				ply:PrintMessage(HUD_PRINTCENTER, "No Shop Near" )
			end
		end
	end
end
concommand.Add("TradeRecive",Transfer2)  